// pages/myCollect/myCollect.js
import checkLogin from "../../utils/checkLogin";
import {getMyCollect} from "../../service/auth";
import {collect} from "../../service/goods";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isFirst:true,
    isLast:false,
    page:1,
    isLoading:false,
    collectLists:[],
    goodsId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },
  getData(){
    this.setData({isLoading: true})
    const {page} = this.data
    getMyCollect(page).then(res=>{
      this.setData({isLoading: false})
      if(this.data.isFirst){
        this.setData({
          isFirst: false
        })
      }
      this.setData({
        collectLists: [...this.data.collectLists,...res.data]
      })
      // console.log(this.data.collectLists)

      //判断是否是最后一页
      if(page == res.meta.pagination.total_pages){
        this.setData({
          isLast: true
        })
      }
    })
  },
  toGoodsInfo(event){
    // console.log(event)
    const id = event.currentTarget.id
    wx.navigateTo({
      url:'/pages/goodsinfo/goodsinfo?id=' + id
    })
  },
  onCollect(event){
    console.log(event)
    collect(event.currentTarget.id).then(()=>{
      wx.showToast({
        title:'已取消收藏',
        icon:'success'
      })
      this.setData({
        collectLists: this.data.collectLists.filter(item=>item.goods_id!=event.currentTarget.id)
      })
    })
  },
  onReachBottom(){
    if(this.data.isLast) return
    this.setData({
      page:this.data.page + 1
    })
    this.getData()
  }
})